﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Tools
{
    /// <summary>
    /// 常用数据类型转换
    /// </summary>
    public static class DataTransaction
    {
        /// <summary>
        /// 得到Sqlite日期类型数据
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetSqliteDate(DateTime? dt)
        {
            if (dt != null)
            {
                return Convert.ToDateTime(((DateTime)dt).ToShortDateString()).ToString("s");
            }
            else
            {
                return DateTime.Now.ToString("s");
            }
          
        }
        /// <summary>
        /// 得到Sqlite日期时间类型数据
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetSqliteDateTime(DateTime? dt)
        {
            if (dt != null)
            {
                return Convert.ToDateTime(((DateTime)dt)).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }

        }

        public static string GetSqliteDateTimeFunction()
        {
            return "(datetime(CURRENT_TIMESTAMP,'localtime'))";
        }
        /// <summary>
        /// 安全转换对象为字符串数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SafeToString(object obj)
        {
            try
            {
                return obj.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 安全转换对象为整数
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int SafeToInt32(object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {
                return 0;
            }

        }
        /// <summary>
        /// 安全转换对象为Decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal SafeToDecimal(object obj)
        {
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 安全转换对象为日期时间
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime? SafeToDateTime(object obj)
        {
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 安全转换对象为布尔值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool SafeToBoolean(object obj)
        {
            try
            {
                if (Convert.ToInt32(obj) == 0)
                    return false;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
